// 页面路径：store/index.js
import Vue from "vue";
import Vuex from "vuex";
import ordering from "./modules/ordering";
import user from "./modules/user";
import page from "./modules/page";
import cart from "./modules/cart";
import order from "./modules/order";
Vue.use(Vuex);

//Vuex.Store 构造器选项
const store = new Vuex.Store({
    modules: {
        ordering,
        user,
        page,
        cart,
        order
    },
    state: {
        // 系统参数
        systemInfo: null,
        // 导航栏高度
        navbarHeight: 0,
        // 搜索框高度
        stickyHeight: 0,
        // 购物车高度
        cartHeight: 0,
        // 没有更多高度
        noMoreHeight: 110,
        // 页面高度
        scrollH: 0,
        // rpx计算参数
        winrate: 0,
    },
    getters: {
        getNavbarHeight: (state, getters) => {
            if (state.navbarHeight == 0) {
                setTimeout(() => {
                    uni.createSelectorQuery()
                        .select(".navbar")
                        .boundingClientRect((data) => {
                            state.navbarHeight = data.height * state.winrate;
                        })
                        .exec();
                }, 0);
            }
            return state.navbarHeight;
        },
        // 搜索框加上navbar的高度
        getStickyHeight: (state, getters) => {
            if (state.stickyHeight == 0) {
                setTimeout(() => {
                    uni.createSelectorQuery()
                        .select(".sticky-tabs")
                        .boundingClientRect((data) => {
                            state.stickyHeight =
                                state.navbarHeight + parseInt(data.height * state.winrate);
                        })
                        .exec();
                }, 0);
            }
            return state.stickyHeight;
        },
    },
    mutations: {
        set(state, data) {
            for (let key in data) {
                state[key] = data[key];
            }
        },
    },
});
export default store;
